package com.ebt.m.customer.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.AppInitService;
import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.utils.ToastUtil;
import com.ebt.m.customer.view.ConfirmDialog;
import com.ebt.m.data.bean.ProposalListItem;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.event.EventRefreshProposalList;
import com.ebt.m.proposal_v2.utils.ProposalUtils;
import com.ebt.m.view.ProposalAllItemView;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Damon on 2017/10/31.
 * 客户计划书列表
 */

public class CustomerProposeListView extends BaseListView<CustomerProposePresenter> {

    public CustomerProposeListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerProposeListView(Context context) {
        this(context, null);
    }

    public CustomerProposeListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mRefreshLayout.setRefreshNeeded(true);
    }

    public IView.OnClickListner onClickListner = new IView.OnClickListner() {
        @Override
        public void onClick(View view, Object... args) {
            ProposalListItem listItem = (ProposalListItem) args[0];
            switch (view.getId()) {
                case R.id.btn_delete:
                    toDelete(listItem.pickNum);
                    break;
                case R.id.content:
                    ProposalUtils.gotoItemFromProposalList(getContext(), listItem);
                    break;
            }
        }

        @Override
        public void onLongClick(View view, Object... args) {

        }
    };

    private void toDelete(String pickNum) {
        ConfirmDialog mConfirmDialog = new ConfirmDialog(getContext());
        mConfirmDialog.setMessage("确定删除吗？")
                .setPositiveButton("删除", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mConfirmDialog.dismiss();
                        deleteProposal(pickNum);
                    }
                }).show();
    }

    @Override
    protected ISectionFactory createViewFactory() {
        return new ISectionFactory() {
            @Override
            public IView createView(int viewType) {
                ProposalAllItemView itemView = new ProposalAllItemView(getContext());
                itemView.setOnClickViewListener(onClickListner);
                return itemView;
            }
        };
    }


    @Override
    protected CustomerProposePresenter createPresenter() {
        return new CustomerProposePresenter(getContext(), this);
    }

    @Override
    public void loadNext() {

    }

    @Override
    public void update(Object... args) {
        super.update(args);
        mPresenter.loadNew(args);

    }

    @Override
    protected boolean createFootView() {
        return false;
    }

    public void deleteProposal(String pickNum) {
        EBTAPI ebtapi = AppInitService.getEbtApi();
        ebtapi.deleteProposal(pickNum)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(responseBodyResponse -> {
                    try {
                        Logger.e(responseBodyResponse.body().string());
                        ToastUtil.show(getContext(), "删除计划书成功");
                        //iView.update(position);
                        EventBus.getDefault().post(new EventRefreshProposalList());
                    } catch (Exception e) {
                        Logger.e(e);
                    }
                }, throwable -> {
                    NetUtils.checkHttpException(getContext(), throwable);
                    Logger.e(throwable.getMessage());
                });
    }

    @Override
    protected View configEmptyView() {
        View emptyContent = LayoutInflater.from(getContext()).inflate(R.layout.common_list_empty_view, null);
        TextView textView = (TextView) emptyContent.findViewById(R.id.tv_empty);
        ImageView img = (ImageView) emptyContent.findViewById(R.id.iv_empty);
        return emptyContent;
    }
}
